package com.doubi.controller.sysUser;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doubi.common.aspectj.annotation.SysLog;
import com.doubi.common.config.shiro.ShiroUtil;
import com.doubi.model.sys.TSysDept;
import com.doubi.model.sys.TSysRole;
import com.doubi.model.sys.TSysUser;
import com.doubi.model.sys.TSysUserRole;
import com.doubi.service.SysDeptService;
import com.doubi.service.SysRoleService;
import com.doubi.service.SysUserRoleService;
import com.doubi.service.SysUserService;
import com.doubi.utils.DataUtil;
import com.doubi.utils.MD5Util;
import com.doubi.vo.AppModel;
import com.doubi.vo.PageVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("sysuser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 功能说明：进入修改用户信息的页面
     * @author  MrDoubi 2019-02-26 15:08:19
     * @param model model
     * @return 页面
     */
    @GetMapping("userData")
    public String userData(Model model){
        TSysUser sysUser = ShiroUtil.getUser();
        model.addAttribute("user",sysUser);
        return "web/user/userData";
    }

    /**
     * 功能说明：修改当前用户资料
     * @author  zhangqy 2019年4月4日16:00:49
     * @return model
     */
    @PostMapping("updateUserDate")
    @ResponseBody
    public AppModel updateUserDate(TSysUser user){
        TSysUser sysUser = ShiroUtil.getUser();
        if(DataUtil.isEmpty(user.getName())){
            return new AppModel(-1,"请填写用户名！");
        }
        if(DataUtil.isEmpty(user.getAccount())){
            return new AppModel(-1,"请填写登录账号！");
        }
        if(DataUtil.isEmpty(user.getHead())){
            return new AppModel(-1,"请上传头像！");
        }
        sysUser = sysUserService.updateUserDate(sysUser,user);
        ShiroUtil.setUser(sysUser);
        return new AppModel(1,"修改用户信息成功！");
    }

    /**
     * 功能说明：进入修改当前用户密码的页面
     * @author  zhangqy 2019年4月4日13:20:41
     * @param model model
     * @return 页面
     */
    @GetMapping("editPwd")
    public String editPwd(Model model){
        TSysUser user = ShiroUtil.getUser();
        model.addAttribute("bean",user);
        return "web/user/editPwd";
    }

    /**
     * 功能说明：修改当前用户密码
     * @author  zhangqy 2019年4月4日13:04:17
     * @return model
     */
    @PostMapping("updatePwd")
    @ResponseBody
    public AppModel updatePwd(String oid, String oldPwd, String newPwd, String newPwd2) throws Exception {
        if (DataUtil.isEmpty(oldPwd)) {
            return new AppModel(-1, "请填写旧密码！");
        }
        if (newPwd.length() < 6 || newPwd.length() > 16) {
            return new AppModel(-1, "密码必须是6到16个字符！");
        }
        if (DataUtil.isEmpty(newPwd)) {
            return new AppModel(-1, "请填写新密码！");
        }
        if (oldPwd.equals(newPwd)) {
            return new AppModel(-1, "旧密码不能与新密码相同！");
        }
        if (!newPwd.equals(newPwd2)) {
            return new AppModel(-1, "两次新密码不相同！");
        }
        TSysUser sysUser = sysUserService.getById(oid);
        if (DataUtil.isEmpty(sysUser)) {
            return new AppModel(-1, "当前标识获取用户失败！");
        }
        if (!sysUser.getPassword().equals(MD5Util.md5Encode(oldPwd))) {
            return new AppModel(-1, "旧密码不正确！");
        }
        try {
            sysUser = sysUserService.updatePwd(sysUser, newPwd);
            ShiroUtil.setUser(sysUser);
            return new AppModel(0, "修改成功！", sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            return new AppModel(0, "修改失败！");
        }
    }


    /**
     * 功能说明：部门和用户的列表
     * @author  MrDoubi 2019-06-28 09:54:36
     * @return 页面
     */
    @GetMapping("userDeptPage")
    @RequiresPermissions("sysuser:userDeptPage")
    public String userDeptPage(){
        return "web/user/userDeptPage";
    }


    /**
     * 功能说明：分页用户列表
     * @author  MrDoubi 2019-06-28 09:05:33
     * @param page page
     * @param sysUser bean
     * @return page
     */
    @SysLog(title = "分页用户列表")
    @PostMapping("getPage")
    @RequiresPermissions("sysuser:getPage")
    @ResponseBody
    public PageVo<TSysUser> getPage(PageVo<TSysUser> page, TSysUser sysUser){
        return sysUserService.getPage(page,sysUser);
    }

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysUserRoleService sysUserRoleService;



    /**
     * 功能说明：用户修改页面
     * @author  MrDoubi 2019-01-10 15:17:48
     * @param model model
     * @param sysUser bean
     * @return 页面
     */
    @RequiresPermissions(value = {"sysuser:edit", "sysuser:add"}, logical = Logical.OR)
    @GetMapping("edit")
    public String userfrom(Model model,TSysUser sysUser,String deptOid){
        if(DataUtil.isNotEmpty(sysUser.getOid())){
            sysUser = sysUserService.getById(sysUser.getOid());
            deptOid = sysUser.getDeptOid();
            QueryWrapper<TSysUserRole> wrapper = new QueryWrapper<>();
            wrapper.eq("user_oid",sysUser.getOid());
            TSysUserRole sysUserRole = sysUserRoleService.getOne(wrapper);;
            model.addAttribute("sysUserRole",sysUserRole);
        }
        if(DataUtil.isNotEmpty(deptOid)){
            model.addAttribute("sysDept",sysDeptService.getById(sysUser.getDeptOid()));
        }
        List<TSysRole> sysRoleList = sysRoleService.list();
        model.addAttribute("bean",sysUser);
        model.addAttribute("sysRoleList",sysRoleList);
        return "web/user/edit";
    }
    /**
     * 功能说明：用户删除
     * @author  MrDoubi 2019-01-10 15:19:14
     * @param sysUser bean
     * @return model
     */
    @SysLog(title = "用户删除")
    @RequiresPermissions("sysuser:delBean")
    @PostMapping("delBean")
    @ResponseBody
    public AppModel delBean(TSysUser sysUser){
        return sysUserService.delBean(sysUser);
    }

    /**
     * 功能说明：重置密码
     * @author  MrDoubi 2019-01-10 15:19:14
     * @param oid bean
     * @return model
     */
    @SysLog(title = "重置密码")
    @RequiresPermissions("sysuser:resetPassword")
    @PostMapping("resetPassword")
    @ResponseBody
    public AppModel resetPassword(String oid){
        return sysUserService.updateResetPassword(oid);
    }

    @SysLog(title = "启用禁用")
    @RequiresPermissions("sysuser:updateAble")
    @PostMapping("updateAble")
    @ResponseBody
    public AppModel updateAble(String oid){
        return sysUserService.updateAble(oid);
    }

    /**
     * MrDoubi
     * 2020年4月1日 13:55:52
     * home 的统计
     */
    @GetMapping("getHomeStatistics")
    @ResponseBody
    public AppModel getHomeStatistics(){
        return sysUserService.getHomeStatistics();
    }
}
